package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Component
@Slf4j
public class Template41 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("37KEY-1")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-3")){
                        String title = data.get("yfFission").get("childTitle").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-5")){
                        String title = data.get("apublic").get("childTitle").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-7")){
                        String title = data.get("yfKoc").get("childTitle").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-9")){
                        String title = data.get("marathon").get("childTitle").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-11")){
                        String title = data.get("wes").get("childTitle").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-13")){
                        textRun.setText(data.get("yfFission").get("clockInCount").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-14")){
                        textRun.setText(data.get("yfFission").get("clockInUsers").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-15")){
                        textRun.setText(data.get("yfFission").get("interactiveRate").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-16")){
                        textRun.setText(data.get("apublic").get("clockInCount").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-17")){
                        textRun.setText(data.get("apublic").get("clockInUsers").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-18")){
                        textRun.setText(data.get("apublic").get("interactiveRate").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-19")){
                        textRun.setText(data.get("yfKoc").get("clockInCount").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-20")){
                        textRun.setText(data.get("yfKoc").get("clockInUsers").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-21")){
                        textRun.setText(data.get("yfKoc").get("interactiveRate").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-22")){
                        textRun.setText(data.get("marathon").get("clockInCount").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-23")){
                        textRun.setText(data.get("marathon").get("clockInUsers").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-24")){
                        textRun.setText(data.get("marathon").get("interactiveRate").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-25")){
                        textRun.setText(data.get("wes").get("clockInCount").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-26")){
                        textRun.setText(data.get("wes").get("clockInUsers").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-27")){
                        textRun.setText(data.get("wes").get("interactiveRate").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-28")){
                        if (data.get("yfFission").get("texts").size() < 1){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("yfFission").get("texts").get(0).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-29")){
                        if (data.get("yfFission").get("texts").size() < 2){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("yfFission").get("texts").get(1).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-30")){
                        if (data.get("yfFission").get("texts").size() < 3){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("yfFission").get("texts").get(2).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-31")){
                        if (data.get("apublic").get("texts").size() < 1){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("apublic").get("texts").get(0).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-32")){
                        if (data.get("apublic").get("texts").size() < 2){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("apublic").get("texts").get(1).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-33")){
                        if (data.get("apublic").get("texts").size() < 3){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("apublic").get("texts").get(2).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-34")){
                        if (data.get("marathon").get("texts").size() < 1){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("marathon").get("texts").get(0).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-35")){
                        if (data.get("marathon").get("texts").size() < 2){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("marathon").get("texts").get(1).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-36")){
                        if (data.get("marathon").get("texts").size() < 3){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("marathon").get("texts").get(2).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-37")){
                        if (data.get("wes").get("texts").size() < 1){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("wes").get("texts").get(0).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-38")){
                        if (data.get("wes").get("texts").size() < 2){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("wes").get("texts").get(1).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-39")){
                        if (data.get("wes").get("texts").size() < 3){
                            textRun.setText("");
                            textParagraph.getTextRuns().get(1).setText("");
                            break;
                        }
                        String title = data.get("wes").get("texts").get(2).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("37KEY-41")){
                        String dataPeriod = data.get("dataPeriod").asText();
                        textRun.setText(dataPeriod);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text Exception:{}",41,e);
                }
            }
        }
    }
}
